import { Effect, Reducer } from 'umi'
import { getAdsList } from "@/api/ads"
import copy from '@/utils'
import { useAddKey } from '@/hooks';


export interface AdsModelState {
  list: Array<any>
}

export interface AdsModelType {
  namespace: 'ads';
  state: AdsModelState;
  effects: {
    init: Effect;
  };
  reducers: {
    save: Reducer<AdsModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<AdsModelState>;
  }
}

const AdsModel: AdsModelType = {
  namespace: 'ads',
  state: {
    list: []
  },

  effects: {
    *init({ payload }, { put }) {
        const data = yield getAdsList()
        yield put({ type: "save", list: data.data })
    },
  },
  reducers: {
    save(state, { list }) {
      const NewState = copy(state)
      NewState.list = useAddKey(list)
      return NewState
    }
  }
};

export default AdsModel;